<template>
  <el-container class="home_container">
    <!-- 头部 -->
    <el-header>
      <div class="header">
        <img class="logo" src="../assets/kgc.jpg" alt="" />
        <span class="title">电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <!-- if判断语句isCollapse为true时宽度为64px,false时200px -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-btn" @click="toggleCollapse()">|||</div>
        <el-menu unique-opened background-color="#373d41"
          text-color="#fff" active-text-color="#268bd2" :collapse='isCollapse'
          :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item @click="saveNavState('/'+subitem.path)" :index="'/'+subitem.path" 
            v-for="subitem in item.children" :key="subitem.id">
              <i class="el-icon-menu"></i>
              {{ subitem.authName }}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //所有菜单数据
      menuList: [],
      iconsObj: {
        125: "el-icon-s-custom",
        103: "el-icon-s-grid",
        101: "el-icon-s-cooperation",
        102: "el-icon-s-order",
        145: "el-icon-s-data",
      },
      //是否折叠
      isCollapse:false,
      //被激活的链接地址
      activePath:''
    };
  },
  created() {
    this.getMenuList();
    this.activePath=window.sessionStorage.getItem("activePath")
  },
  methods: {
    loginOut() {
      //退出功能 清空token 重定向到login页面
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取所有菜单
    async getMenuList() {
      //解构赋值data并重命名为res data为服务器返回的数据
      const { data: res } = await this.$http.get("menus");
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
    //点击按钮菜单折叠与展开
    toggleCollapse(){
      this.isCollapse=!this.isCollapse
    },
    //保存链接的激活状态  保存到sessionStorage
    saveNavState(activePath){
      window.sessionStorage.setItem("activePath",activePath)
      //点击后给链接重新赋值
      this.activePath=activePath
    }
  }
};
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding: 0 15px 0 10px;
    align-items: center;
  }
  .el-aside {
    background-color: #333744;
    .toggle-btn {
      background-color: #4a5064;
      color: white;
      font-size: 10px;
      display: flex;
      justify-content: center;
      line-height: 24px;
      cursor: pointer;
      letter-spacing: .2em;
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
  .header {
    display: flex;
    align-items: center;
    .title {
      font-size: 24px;
      font-weight: bold;
      margin-left: 15px;
      color: white;
    }
    .logo {
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }
  }
}
</style>

